# -*- coding: utf_8 -*-
# @Time : 2021/8/10 0010 16:45
# @Author : lyw
# @Remark : 数据源model
import re
import time

from pydantic import validator
from superbsapi import EncryptString
from mxsoftpy import Model


class ActiveRequestModel(Model):
    create_time: int = int(time.time())
    desc: str = ""
    host: str
    is_del: int = 0
    label: str
    model_id: str = ""
    password: str
    port: int
    state: int = 1
    update_time: int = 0
    user_name: str
    count: int = 0
    type_id: str

    @validator('label')
    def check_label(cls, value):
        assert value != "", "label参数不存在..."
        return value

    @validator('user_name')
    def check_user_name(cls, value):
        assert value != "", "user_name参数不存在..."
        return value

    @validator('password')
    def check_password(cls, value):
        assert value != "", "password参数不存在..."
        # return value
        return EncryptString(value)[1]

    @validator('host')
    def check_host(cls, value):
        pattern = re.compile(r'^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$')
        re_str = pattern.match(value)
        if re_str:
            return re_str.group()
        else:
            raise AssertionError("IP地址格式不正取...")

    @validator('port')
    def check_port(cls, value):
        pattern = re.compile(
            r'^[1-9]$|(^[1-9][0-9]$)|(^[1-9][0-9][0-9]$)|(^[1-9][0-9][0-9][0-9]$)|(^[1-6][0-5][0-5][0-3][0-5]$)')
        re_str = pattern.match(str(value))
        if re_str:
            return re_str.group()
        else:
            raise AssertionError("端口格式不正取...")

    # @validator('model_id')
    # def check_model_id(cls, value):
    #     assert value.isdigit(), "origin_id参数不正确..."
    #     return value


class DevicePushModel(Model):
    create_time: int = int(time.time())
    desc: str
    is_del: int = 0
    label: str
    host: str
    model_id: str = ""
    state: int = 1
    update_time: int = 0
    count: int = 0
    type_id: str

    # @validator('model_id')
    # def check_model_id(cls, value):
    #     assert value.isdigit(), "origin_id参数不正确..."
    #     return value

    @validator('label')
    def check_label(cls, value):
        assert value != "", "label参数不存在..."
        return value


class ScriptPushModel(Model):
    create_time: int = int(time.time())
    desc: str
    host: str
    is_del: int = 0
    label: str
    model_id: str = ""
    state: int = 1
    update_time: int = 0
    count: int = 0
    type_id: str

    @validator('host')
    def check_host(cls, value):
        pattern = re.compile(r'^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$')
        re_str = pattern.match(value)
        if re_str:
            return re_str.group()
        else:
            raise AssertionError("IP地址格式不正取...")

    # @validator('model_id')
    # def check_model_id(cls, value):
    #     assert value.isdigit(), "origin_id参数不正确..."
    #     return value

    @validator('label')
    def check_label(cls, value):
        assert value != "", "label参数不存在..."
        return value
